package com.ynet.hmds28.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.ynet.hm.user.service.PlatformFlowService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hmds28.user.dao.PlatflowDao;
import com.ynet.hmds28.user.dao.ServiceconsumeDao;
import com.ynet.hmds28.user.dao.ServiceprovideDao;
import com.ynet.hmds28.user.model.Platflow;
import com.ynet.hmds28.user.model.Serviceconsume;
import com.ynet.hmds28.user.model.ServiceconsumeQuery;
import com.ynet.hmds28.user.model.Serviceprovide;

@Transactional
@HSFProvider(serviceInterface = PlatformFlowService.class, serviceVersion = "1.0.0", serviceGroup = "hmds28")
public class PlatformFlowServiceImpl implements PlatformFlowService {
	@Autowired
	private PlatflowDao platflowDao;
	@Autowired
	private ServiceconsumeDao serviceconsumeDao;
	@Autowired
	private ServiceprovideDao serviceprovideDao;

	@Override
	public Result<?> platformFlow(String flowId, String inString,
			String outString, String merchantNo, String provideId,
			String isSuccess, long startTime) {
		// 开启事务
		platflowDao.enableTXC();

		ServiceconsumeQuery sc = new ServiceconsumeQuery();
		sc.createCriteria().andSeviceproviceidEqualTo(provideId);
		List<Serviceconsume> sclist = serviceconsumeDao.selectByExample(sc);

		Serviceprovide sp = serviceprovideDao.selectByPrimaryKey(provideId);

		// TODO 开启GTS事务,记录平台流水
		Platflow record = new Platflow();
		record.setCalltime(new Date());
		record.setConsumeuserid(merchantNo);
		record.setExportmessage(outString);
		record.setImportmessage(inString);
		record.setPlatflowid(flowId);
		record.setProviceuserid(sp.getUserid());
		record.setSerivceprovideid(provideId);
		record.setServiceconsumeid(sclist.get(0).getServiceconsumeid());
		record.setIssuccess(isSuccess);
		long endTime = System.currentTimeMillis();
		long makeTime = endTime - startTime;
		record.setMaketime(makeTime + "");
		platflowDao.insert(record);
		return ResultUtil.success();
	}

}
